package com.douqu.game.core.entity.ext.data.timeLimit;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.goods.PropConfig;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.db.DB;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.CoreUtils;

/**
 * @author wangzhenfei
 *         2018-04-18 11:33
 *         时效性道具
 */
public class TimePropDB extends DB{
    /**
     * 唯一标识
     */
    public String uniqueId;
    /**
     * 结束时间
     */
    public long endTime;

    /**
     *  是否被使用
     */
    public boolean isUse;

    /**
     * 展示结束时间
     */
    public long endShowTime;

    public TimePropDB() {
        super(DataFactory.PROP_KEY);
    }

    public TimePropDB(int id, String uniqueId) {
            super(DataFactory.PROP_KEY, id);
        this.uniqueId = uniqueId;
        PropConfig config = getConfig();
        this.endTime = CoreUtils.getNDaysAfter(DataFactory.currentTime, config.timePropData.value);
        //结束展示时间比结束时间晚三天
        this.endShowTime = CoreUtils.getNDaysAfter(DataFactory.currentTime, config.timePropData.value + 3);
    }

    public boolean isCanDelete(){
        return endShowTime != 0 && DataFactory.currentTime > endShowTime;
    }

    public boolean isTimeIn(){
        return DataFactory.currentTime < endTime;
    }

    public boolean isCanUse(){
        return !isUse && isTimeIn();
    }

    public void useTimeProp(Player player){
        if(isCanUse()){
            player.addChangeTimeGoods(SGCommonProto.E_GOODS_TYPE.GOODS_TYPE_PROPS, id, -1, 1,uniqueId,
                    endTime, false);
            isUse = true;
            endShowTime = CoreUtils.getNDaysAfter(DataFactory.currentTime, 3);
        }
    }


    @Override
    public void reset() {

    }

    @Override
    public void writeTo(ByteBuffer buffer)
    {
        super.writeTo(buffer);
        buffer.writeUTF(uniqueId);
        buffer.writeLong(endTime);
        buffer.writeBoolean(isUse);
        buffer.writeLong(endShowTime);
        buffer.writeBoolean(false);
    }

    @Override
    public void loadFrom(ByteBuffer buffer)
    {
        super.loadFrom(buffer);
        uniqueId = buffer.readUTF();
        endTime = buffer.readLong();
        isUse = buffer.readBoolean();
        endShowTime = buffer.readLong();
        buffer.readBoolean();
    }

    @Override
    public PropConfig getConfig() {
        return (PropConfig) super.getConfig();
    }




}
